﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using WebComponents.Data;
using WebComponents.Security;
using GBIT.Web;
using WebComponents;

public partial class Content : UserControlBase {
  protected void Page_Load(object sender, EventArgs e) {
    if (!Page.IsPostBack) {
      var list = PageDescriptionController.GetByCultureId("vi-VN");
      cmbMucTin.DataSource = list;
      cmbMucTin.DataValueField = "PageID";
      cmbMucTin.DataTextField = "PageTitle";
      cmbMucTin.DataBind();
      //PageDescriptionInfo pageDesctiption = PageDescriptionController.GetByPageName("GioiThieu", "vi-VN");
      //if (pageDesctiption != null) {
      //  txtTitle.Text = pageDesctiption.PageTitle;
      //  txtContent.Value = pageDesctiption.FullDescription;
      //}
      if (cmbMucTin.Items.Count > 0)
        LoadNewsInfo();
    }
    this.SaveEnabled = true;
    this.Title = "Nội dung chính";
    this.TaskTitle = "Cập nhật";
  }
  protected void cmbLanguage_SelectedIndexChanged(object sender, EventArgs e) {
    LoadNewsInfo();
  }
  private void LoadNewsInfo() {
    PageDescriptionInfo pageDesctiption = PageDescriptionController.GetByPageId(ToInt(cmbMucTin.SelectedValue), cmbLanguage.SelectedValue);
    if (pageDesctiption != null) {
      txtTitle.Text = pageDesctiption.PageTitle;
      txtContent.Value = pageDesctiption.FullDescription;
    } else {
      txtTitle.Text = "";
      txtContent.Value = "";
    }
  }
  protected override void OnSaveRecord() {
    PageDescriptionInfo pageDesctiption = PageDescriptionController.GetByPageId(ToInt(cmbMucTin.SelectedValue), cmbLanguage.SelectedValue);
    if (pageDesctiption != null) {
      pageDesctiption.PageTitle = txtTitle.Text;
      pageDesctiption.FullDescription = txtContent.Value;
      PageDescriptionController.Update(pageDesctiption);
    }    
    Security.ShowClientMessage(Page, "Cập nhật thông tin thành công");
  }
  protected void cmbMucTin_SelectedIndexChanged(object sender, EventArgs e) {
    LoadNewsInfo();
  }
}
